#!/usr/bin/env python
# -*- coding: utf-8 -*-
#import matplotlib.pyplot as plt
#import numpy as np

import os
import glob
import tool_europa as te
import tool_math as tm
import cPickle as pickle

path_pickle = 'out.pickle'
d = pickle.load(open(path_pickle))
selected_languages = d.keys()

def cmp_lg(x,y) :
  totalx = d[x]['total']
  totaly = d[y]['total']
  return totalx - totaly

selected_languages.sort(cmp_lg)

res = []
nb_elt_per_line = 6
nb_line = len(selected_languages) / nb_elt_per_line + 1
nb_elt_last_line = len(selected_languages) % nb_elt_per_line
#print nb_line, nb_elt_last_line,len(selected_languages)

res = [[('',0) for j in xrange(nb_elt_per_line)] for i in xrange(nb_line)]

for i,lg in enumerate(selected_languages) :
  l = i/nb_elt_per_line
  col = i%nb_elt_per_line
  res[l][col] = (lg,d[lg]['total'])

out = ''

for l, line in enumerate(res) :
  out_line_title = ''
  out_line_val = ''
  for c, val in enumerate(line) :
    h = val[0] if val[0] != '' else ''
    v = val[1] if val[1] != 0 else ''
    out_line_title += '<th>%s</th>'%h
    out_line_val += '<td>%s</td>'%v
  out += '<tr>%s</tr>\n'%(out_line_title)
  out += '<tr>%s</tr>\n'%(out_line_val)

print out
